package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Field;
import com.portal.logic.FieldDAO;
import com.portal.logic.SerializationUtil;

public class AddFieldScr extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldName;


	/**
	 * Create the frame.
	 */
	public AddFieldScr() {
		setTitle("Add Field");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(55, 163, 97, 25);
		contentPane.add(btnBack);

		JLabel lblFieldName = new JLabel("Field Name");
		lblFieldName.setBounds(42, 53, 73, 16);
		contentPane.add(lblFieldName);

		textFieldName = new JTextField();
		textFieldName.setBounds(127, 50, 116, 22);
		contentPane.add(textFieldName);
		textFieldName.setColumns(10);

		JLabel lblNewLabel = new JLabel("Enter Name of new Field:");
		lblNewLabel.setBounds(42, 13, 274, 16);
		contentPane.add(lblNewLabel);

		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				String name = textFieldName.getText();
				if(name == null || ("".equals(name))){
					JOptionPane.showMessageDialog(null, "New Field text can not be empty.");
				}else{
					Connection connection = ConnectionManager.getConnection();
					if(connection != null){
						FieldDAO fieldDAO = new FieldDAO(connection) ;
						Field field = fieldDAO.selectByName(name);
						if(name.equalsIgnoreCase(field.getName())){
							JOptionPane.showMessageDialog(null, "Such Field already exists.");
						}else{
							int maxFieldId = fieldDAO.selectMaxId();
							System.out.println(maxFieldId);
							Field addField = new Field(maxFieldId+1, name);
							
							try {
								SerializationUtil.serialize(addField, "c:\\field.out");
							} catch (IOException e) {
								e.printStackTrace();
							}
							
							fieldDAO.insert();
							JOptionPane.showMessageDialog(null, "New Field was added!");
						}
					}else{
						JOptionPane.showMessageDialog(null, "There is no JDBC Connection.");
					}
				}
			}
		});
		btnSubmit.setBounds(284, 49, 97, 25);
		contentPane.add(btnSubmit);
	}

}
